package com.zmn.plat.services.impl.part;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.zmn.common.dto.StatusDTO;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.pager.LayuiPageUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.manager.dictionary.PartDict;
import com.zmn.plat.manager.interfaces.cache.PartCategoryCache;
import com.zmn.plat.model.entity.part.PartCategory;
import com.zmn.plat.model.entity.part.PartCategoryQuery;
import com.zmn.plat.persistence.interfaces.part.PartCategoryDAO;
import com.zmn.plat.services.interfaces.part.PartCategoryService;

/**
 * 类描述 PartCategoryServiceImpl
 *
 * @author DaiZhichao
 * @since 2018/10/19 12:03
 */

@Service
public class PartCategoryServiceImpl implements PartCategoryService {

    @Resource
    private PartCategoryDAO dao;
    @Resource
    private PartCategoryCache categoryCache;

    @Override
    public PartCategory findByKey(Integer id) {
        return dao.findByKey(id);
    }

    @Override
    public List<PartCategory> listPageByQuery(PartCategoryQuery query) {
        Integer count = countByQuery(new LayuiPageUtil<>(query).setLayuiPageBlank().setStartEndTime().getQuery());
        query.setTotalCount(count);
        return dao.listPageByQuery(new LayuiPageUtil<>(query).setLayuiPage().setStartEndTime().getQuery());
    }

    @Override
    public List<VtDTO> getPartCategorySimpleList(Integer parentId) {
        // return categoryCache.getPartCategorySimpleList(parentId);
        PartCategoryQuery query = new PartCategoryQuery();
        query.setParentId(parentId);
        return dao.listVtDTOByQuery(query);
    }

    @Override
    public List<PartCategory> listByParentId(Integer parentId, Short status) {
        PartCategoryQuery query = new PartCategoryQuery();
        query.setParentId(parentId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<PartCategory> listByQuery(PartCategoryQuery query) {
        return dao.listByQuery(new LayuiPageUtil<>(query).setLayuiPage().setStartEndTime().getQuery());
    }

    @Override
    public Integer countByQuery(PartCategoryQuery query) {
        return query == null ? 0 : dao.countByQuery(new LayuiPageUtil<>(query).setLayuiPageBlank().setStartEndTime().getQuery());
    }

    @Override
    @CacheEvict(cacheNames = "redis1h", key = "'part:category:parentId:'+#entity.parentId", condition = "#entity.categId > 0")
    public Integer updateByKey(PartCategory entity, LoginStaff mcStaff) {
        this.maintainFullName(entity);
        entity.setUpdater(mcStaff.getRealName());
        entity.setUpdateTime(DateUtil.getNow());
        return dao.updateByKey(entity);
    }

    /**
     * 描述: 启用 status：1停用 2启用
     *
     * @param categId
     *            ID
     * @return categId
     * @author DaiZhichao
     * @since 2018/10/19 11:53
     */
    @Override
    public Integer updateStatus(Integer categId, Short status, String updater) {
        PartCategory entity = new PartCategory();
        entity.setCategId(categId);
        entity.setStatus(status);
        entity.setUpdater(updater);
        return updateStatus(entity);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> categIds, Integer status, String updater) {
        return updateStatusBatch(new StatusDTO() {
            {
                setIds(categIds);
                setStatus(status);
                setUpdater(updater);
            }
        });
    }

    @Override
    public Integer updateSort(Integer categId, Integer sort) {
        PartCategory entity = new PartCategory();
        entity.setCategId(categId);
        entity.setSort(sort);
        return updateSort(entity);
    }

    @Override
    public Integer insert(PartCategory entity, LoginStaff mcStaff) {
        this.maintainFullName(entity);
        entity.setCreater(mcStaff.getRealName());
        entity.setUpdater(mcStaff.getRealName());
        entity.setUpdateTime(DateUtil.getNow());
        entity.setCreateTime(entity.getUpdateTime());
        return dao.insert(entity);
    }

    @Override
    public void deleteByKey(Integer categId) {
        if (categId != null) {
            dao.deleteByKey(categId);
        }
    }

    @CacheEvict(cacheNames = "redis1h", key = "'part:category:parentId:'+#entity.parentId", condition = "#entity.categId > 0")
    public Integer updateStatus(PartCategory entity) {
        entity.setUpdateTime(DateUtil.getNow());
        return dao.updateStatus(entity);
    }

    /**
     * 描述: updateSort Base方法
     *
     * @param entity
     *            PartCategory
     * @return Integer
     * @author DaiZhichao
     * @since 2018/12/14 13:05
     */
    private Integer updateSort(PartCategory entity) {
        return dao.updateSort(entity);
    }

    /**
     * 描述: updateStatusBatch Base方法
     *
     * @param dto
     *            StatusDTO
     * @return Integer
     * @author DaiZhichao
     * @since 2018/12/14 13:05
     */
    private Integer updateStatusBatch(StatusDTO dto) {
        dto.setUpdateTime(DateUtil.getNow());
        return dao.updateStatusBatch(dto);
    }

    /**
     * 描述:维护配件类型全名
     *
     * @param partCategory
     *            PartCategory
     * @return PartCategory
     * @author DaiZhichao
     * @since 2018/11/9 14:30
     */
    private PartCategory maintainFullName(PartCategory partCategory) {

        Integer parentId = partCategory.getParentId();
        if (Objects.isNull(parentId)) {
            return partCategory;
        }

        if (Objects.equals(parentId, PartDict.PART_CATEGORY_ROOT_PARENTID)) {
            partCategory.setFullName(partCategory.getName());
        } else {
            PartCategory parentCategory = dao.findByKey(parentId);
            String sb = parentCategory.getName() + "-" + partCategory.getName();
            partCategory.setFullName(sb);
        }

        return partCategory;
    }

}
